import qs from 'qs'
import axios from 'axios'
import { getToken } from '@/utils/v2/auth'
/*
 * 公用导出
 * */
export function exportExcel(url, params, fileName, type) {
  if (type != 'json') {
    params = qs.stringify(params)
  }
  axios
    .post('/xunfei-api' + url, params, {
      responseType: 'blob',
      headers: {
        Authorization: getToken() || ''
      }
    })
    .then((res) => {
      const link = document.createElement('a')
      let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      link.download = fileName //下载的文件名
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    })
    .catch((err) => {
      //console.log(err)
    })
}

export function downloadExcel(url, fileName) {
  axios
    .post(url, {
      responseType: 'blob',
      headers: {
        Authorization: getToken() || ''
      }
    })
    .then((res) => {
      const link = document.createElement('a')
      let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      link.download = fileName //下载的文件名
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    })
    .catch((err) => {
      console.log(err)
    })
}


export function downloadWord(url, fileName) {
  axios
    .get(url, {
      responseType: 'blob',
      headers: {
        Authorization: getToken() || ''
      }
    })
    .then((res) => {
      const link = document.createElement('a')
      let blob = new Blob([res.data], {
        type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      })
      link.style.display = 'none'
      link.href = URL.createObjectURL(blob)
      link.download = fileName //下载的文件名
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    })
    .catch((err) => {
      console.log(err)
    })
}
